package com.catazy.base.presentation.compose.composable

import androidx.annotation.RawRes
import androidx.annotation.StringRes
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.requiredSize
import androidx.compose.foundation.layout.wrapContentSize
import androidx.compose.material3.Card
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import com.airbnb.lottie.compose.LottieAnimation
import com.airbnb.lottie.compose.LottieCompositionSpec
import com.airbnb.lottie.compose.rememberLottieComposition
import com.catazy.base.common.res.Dimen

@Composable
fun LabeledAnimation(@StringRes label: Int, @RawRes assetResId: Int) {
    Card(
        modifier = Modifier.wrapContentSize()
    ) {
        Column(
            // 水平居中
            horizontalAlignment = Alignment.CenterHorizontally,
            // Modifier：用于修饰和配置 Compose 组件的行为和外观。
            modifier = Modifier
                // 组件包裹其内容的最小尺寸
                .wrapContentSize()
                // padding边距
                .padding(Dimen.spaceXL)
        ) {
            TextTitleMedium(text = stringResource(id = label))
            LottieAssetLoader(assetResId)
        }
    }
}

@Composable
fun LottieAssetLoader(@RawRes assetResId: Int) {
    // rememberLottieComposition： 该函数负责异步加载 Lottie 动画的组成，并将其存储在内存中，以便在重组时重复使用
    // LottieCompositionSpec.RawRes(assetResId): 指定 Lottie 动画的资源 ID
    val composition by rememberLottieComposition(LottieCompositionSpec.RawRes(assetResId))

    LottieAnimation(
        composition = composition,
        // 设置组件的固定尺寸
        modifier = Modifier.requiredSize(Dimen.imageSize)
    )
}